package com.ewins.weatherdataclient.monitorpoolmodule;

import com.ewins.weatherdataclient.downloadservice.FileDownloadMonitorHttp;
import com.ewins.weatherdataclient.interfacedatamodule.DownloadInfos;
import com.ewins.weatherdatautils.bean.DownLoadFileInfo;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by lenovo on 2015/12/28.
 */
public class MonitorPoolModuleHttp {

    ThreadPoolExecutor monitorPool;
    EventBus eb;

    public MonitorPoolModuleHttp(EventBus eb, ThreadPoolExecutor monitorPool){

        this.monitorPool=monitorPool;
        this.eb=eb;
        eb.register(this);
    }

    @Subscribe
    public void onMessage(Object o) {
        // TODO Auto-generated method stub

        if(o instanceof DownloadInfos){
            try {
                DownloadInfos downLoadInfo = (DownloadInfos) o;
                FileDownloadMonitorHttp fileDownLoad = new FileDownloadMonitorHttp(new EventBus());
                DownLoadFileInfo needLoadFile = downLoadInfo.getNeedLoadFile();
                if (needLoadFile.getStartDownLoadTime() == null) {
                    needLoadFile.setStartDownLoadTime(new DateTime(DateTimeZone.UTC));
                }
                fileDownLoad.setFileInfo(needLoadFile);
                fileDownLoad.setOnChangedInfo(downLoadInfo.getOnChangedInfo());
                monitorPool.execute(fileDownLoad);
            }
            catch(Exception e){

                e.printStackTrace();
            }
        }
    }

}
